$(document).ready(function () {
    //背景保存
    var data = readCookie('skin');
    changeSkin(data);
    //天气预报下拉框的出现隐藏
    $(".pre").mouseenter(function () {
        $(".prediction-box").show();
    }).mouseleave(function () {
        $(".prediction-box").hide();
    });
    //天气预报下拉框 鼠标放上的变化
    $(".fivedays li").mouseenter(function () {
        $(this).find("a").css("color", "#38f");
    }).mouseleave(function () {
        $(this).find("a").css("color", "#666");
    });
    var d = new Date();
    $(".date1").text(d.getMonth() + 1 + "月" + d.getDate() + "日");
    var china_today = LunarDate.GetLunarDay(d.getFullYear(), d.getMonth() + 1, d.getDate());
    $(".date2").text("农历" + china_today);
    //获取天气预报
    $.ajax({
        type: "get",
        url: "http://wthrcdn.etouch.cn/weather_mini",
        data: {city: "上海"},
        dataType: 'json'
    })
        .done(function (msg) {
            if (msg.desc == "OK") {
                $(".city").text(msg.data.city + "：");
                switch (msg.data.forecast[0].type) {
                    case "雨夹雪":
                        $(".now-icon").attr("src", "b/a6.png");
                        break;
                    case "小雨" :
                        $(".now-icon").attr("src", "b/a3.png");
                        break;
                    case "晴" :
                        $(".now-icon").attr("src", "b/a0.png");
                        break;
                    default :
                        $(".now-icon").attr("src", "b/a2.png");
                }
                $(".temp").text(msg.data.wendu + "℃");
                $(".wind").text(msg.data.forecast[0].fengli);
                $(".air").text(msg.data.aqi);
                $(".dat").each(function (index, value) {
                    $(value).text('(' + msg.data.forecast[index].date + ')').attr("title", msg.data.forecast[index].date)
                });
                $(".tem").each(function (index, value) {
                    $(value).text(msg.data.forecast[index].high + msg.data.forecast[index].low).attr("title", msg.data.forecast[index].high + msg.data.forecast[index].low)
                });
                $(".wea").each(function (index, value) {
                    $(value).text(msg.data.forecast[index].type).attr("title", msg.data.forecast[index].type);
                });
                $(".win").each(function (index, value) {
                    $(value).text(msg.data.forecast[index].fengxiang + msg.data.forecast[index].fengli).attr("title", msg.data.forecast[index].fengxiang + msg.data.forecast[index].fengli)
                });
                $(msg.data.forecast).each(function (index) {
                    var li = $(".fivedays li");
                    switch (msg.data.forecast[index].type) {
                        case "多云":
                            li.eq(index).find(".pic").attr("src", "b/a2big.jpg");
                            break;
                        case "小雨":
                            li.eq(index).find(".pic").attr("src", "b/a7big.jpg");
                            break;
                        case "霾":
                            li.eq(index).find(".pic").attr("src", "b/7.png");
                            break;
                        case "多云转阴":
                            li.eq(index).find(".pic").attr("src", "b/a1.jpg");
                            break;
                        case "阴":
                            li.eq(index).find(".pic").attr("src", "b/a1.jpg");
                            break;
                        default :
                            li.eq(index).find(".pic").attr("src", "b/a0big.jpg");
                    }
                });
            }
        });

    //输入框获取焦点
    $('.input').focus();
    //头部 消息
    $(document).click(function (e) {
        var container = $(".infobox");
        var msgbtn = $(".tidings");
        if (msgbtn.is(e.target)) {
            container.toggle();
            return;
        }
        if (!container.is(e.target) && container.has(e.target).length === 0) {
            container.hide();
        }
    });

    //点击”换肤“按钮 她下面的隐藏框的出现 隐藏
    $(document).click(function (e) {
        var container = $(".skin");
        var msgbtn = $(".retract");
        var gg = $(".skin-peeler");
        if (gg.is(e.target)) {
            container.slideDown(500);
            return;
        }
        if (msgbtn.is(e.target)) {
            container.slideUp();
            return;
        }
        if (!container.is(e.target) && container.has(e.target).length === 0) {
            container.slideUp();
        }
    });
    //换肤隐藏框内图片组的变化
    $(".stylename li").each(function (index, value) {
        $(value).on('click', function () {
            $(".skinshow").removeClass('skinshow');
            $('.bagimg').children('div').eq(index).addClass('skinshow');
        })
    });
    //读取皮肤
    function changeSkin(data) {
        if (data == undefined || data == 'white') {
            $('#container ').css("background", "#fff");
            $(".css").attr('href', 'default.css');
            $(".art-top img").attr('src', 'b/bd_logo1.png');
        } else {
            $('#container ').css({
                "backgroundColor": "rgb(64, 64, 64)",
                "backgroundImage": "url(" + data + ")",
                "backgroundRepeat": "no-repeat",
                "backgroundPosition":"center 0",
                "background-size": "cover",
                "background-attachment":"fixed"
            });
            $(".css").attr('href', 'skin.css');
            $(".art-top img").attr('src', 'b/logo_white.png');
        }
    }

    //点击default背景白色
    $(".default").on('click', function () {
        $('#container ').css(
            "background", "#fff"
        );
        createCookie('skin', 'white', 8);
        changeSkin('white');
    });
    //点击图片 更换背景图片
    $(".bagimg img").each(function () {
        $(this).on('click', function () {
            var src = $(this).attr('src');
            var s = src.substring(2);
            var bg = "b/" + "a" + s;
            $('#container ').css({
                "backgroundColor": "rgb(64, 64, 64)",
                "backgroundImage": "url(" + bg + ")",
                "backgroundRepeat": "no-repeat",
                "backgroundPosition":"center 0",
                "background-size": "cover",
                "background-attachment":"fixed"
            });
            createCookie('skin', bg, 8);
            changeSkin(bg);
        })
    });
    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name, "", -1);
    }

    //设置
    $(".set").mouseenter(function () {
        $(".dropdown1").show();
    }).mouseleave(function () {
        $(".dropdown1").hide();
    });

    //更多
    $("#more").mouseover(function () {
        $(".dropdown2").show();
    });
    $(".dropdown2").mouseleave(function () {
        $(".dropdown2").hide();
    });
    //四个标题
    $(".caption div").each(function (index, value) {
        $(value).on("click", function () {
            $(".grey").removeClass("grey");
            $(this).addClass("grey");
            var id = $(this).attr("id");
            $(".showyou").removeClass("showyou");
            var sh = $(".listinner").children("li").eq(index);
            sh.addClass("showyou");
            sh.siblings().removeClass("showyou");
            createCookie('show', id, 8);
        });
    });
    var rr = readCookie('show');
    if (rr == null) {
        $('#follow').click();
    } else {
        $('#' + rr).click();
    }

    //我的关注 里的框框
    $(".navbartext li").each(function () {
        $(this).mouseenter(function () {
            $(this).css("border", "1px solid #f0f0f0");
        }).mouseleave(function () {
            $(this).css("border", "1px solid transparent");
        })
    });
    //我的关注第一行
    $(".website-list li").filter(".orange").each(function () {
        $(this).hover(function () {
            $(this).find(".rebate").toggle();
        })
    });
    //回到顶部
    $(".fix").mouseenter(function () {
        $(".on").removeClass("on");
        $(".return2").addClass("on");
    }).mouseleave(function () {
        $(".return1").addClass("on");
        $(".return2").removeClass("on");
    });

    $(".fix").on("click", function () {
        $("body,html").animate({scrollTop:0},500);
    });
    var moreimg = {
        "data": [{
            "src": "95510.png"
        }, {
            "src": "95488.png"
        }, {
            "src": "7656.png"
        }, {
            "src": "9321.png"
        }, {
            "src": "200001.png"
        }, {
            "src": "95511.png"
        }]
    };
    var span1 = ["58同城", "新浪微博", "百度VIP", "微软商店", "斗鱼", "腾讯"];
    var href1 = ['http://sh.58.com/',
        'http://www.weibo.com/',
        'http://vip.baidu.com/',
        'https://www.microsoft.com/',
        'http://www.douyutv.com/',
        'http://www.qq.com/'];
    $(window).scroll(function () {
        console.log('aa');
        var scrollHeight = $(document).scrollTop();
        var height = $(window).height();
        //回到顶端出现 隐藏
        if (scrollHeight > 0) {
            $(".fix").css("display", "block");
        } else {
            $(".fix").css("display", "none");
        }
        //窗口顶部的搜索框的出现隐藏
        if (scrollHeight > 180) {
            $(".topsearch").css('display', 'block');
            $(".topsearch").find('.input').focus();

        } else {
            $(".topsearch").css('display', 'none');
        }
        if(scrollHeight >= $(".main").offset().top){
            // console.log("aa");
            // $(".listinner li").css({"position":"fixed",'top':'94px','left':'50%','margin-left':'-450px'})
        }
        //”推荐“的右侧
        if (scrollHeight >= 250) {
            $(".flogt").css({
                'position': 'fixed', 'top': '100px',
                'right': '211px'
            })
        } else {
            $(".flogt").css({'position': 'absolute', 'top': '25px', 'right': '16px'});
        }
        //动态记载内容

        if ($(document).scrollTop() + height >= $(document).height()) {
            $("footer").hide();
            $(".rect").hide();
            //我的关注
            $("<h3>").text("资讯信息").appendTo($(".likelist"));
            var ula = $("<ul>").addClass("website-list").appendTo($(".likelist"));
            $.each(moreimg.data, function (index, value) {
                var li = $("<li>").appendTo(ula);
                var a = $("<a>").attr({'title': $(span1)[index], "href": $(href1)[index]}).appendTo(li);
                $("<img>").attr("src", "b/" + $(value).attr("src")).appendTo(a);
                $("<span>").text($(span1)[index]).appendTo(a);
            });
            //”推荐“
            var dd = ['网易新闻　01-12 08:22',
                '搜狐新闻　01-12 9:22',
                '新浪娱乐　01-12 11:52'];
            var span2 = ['广东男子携半吨钞票买车',
                '安徽中学现多例肺结核引发恐慌 学生向省长投诉',
                '七旬婆婆跟儿子到酒吧“驻唱”'];
            var link1 = ['http://money.163.com/16/0113/07/BD6O65CL00251LKI.html',
                'http://money.163.com/16/0113/07/BD6O65CL00251LKI.html',
                'http://ent.sina.com.cn/v/h/2016-01-09/doc-ifxnkkuv4265192.shtml'];
            $.each(span2, function (index, value) {
                var li = $("<li>").addClass("secli").appendTo($(".recommendlt"));
                var dl = $("<dl>").appendTo(li);
                var dt = $("<dt>").appendTo(dl);
                var a = $("<a>").attr({'title': value, 'href': $(link1)[index]}).appendTo(dt);
                $("<dd>").text($(dd)[index]).appendTo(dl);
                $("<span>").text(value).appendTo(a);

            });
            //视频
            var img3 = ["u=354.jpg", "u=34831.jpg", "u=355.jpg", "u=4085.jpg", "u=156.jpg", "u=2428.jpg"];
            var ul3 = $("<ul>").addClass('radioul').appendTo(".radioinner");
            var a3 = ["奔跑吧兄弟第二季", "天天向上", "汽车冰雪路上漂移转弯 撞飞对向车零件散落一地",
                "国民党主席之争开打 洪秀柱或成可能人选", "TWICE出道专辑《Like OOH-AHH》全辑试听",
                "《音乐银行》TWICE《优雅地》现场151113"];
            var spanarr = ["6.7", "7.4"];
            $.each(img3, function (index, value) {
                var li3 = $("<li>").addClass('top-info').appendTo(ul3);
                var a = $("<a>").appendTo(li3);
                $("<img>").attr('src', 'b/' + img3[index]).appendTo(a);
                var div3 = $("<div>").addClass('fontinfo').appendTo(li3);
                $("<a>").addClass('text').text(a3[index]).attr('title', a3[index]).appendTo(div3);
                var div31 = $("<div>").addClass('scole1').appendTo(div3);
                var span3 = $("<span>").addClass('wstar').appendTo(div31);
                var span31 = $("<span>").addClass('bstar').appendTo(span3);
                var span32 = $("<span>").text(spanarr[index]).appendTo(div31);
            });
            //购物
            var img4 = ['2cdd.jpg', 'ff55.jpg', '9498.jpg', 'cd14.jpg', '1493.jpg', '653.jpg', 'cf3.jpg', 'e258.jpg'];
            var money = ['¥ 80', '¥ 25.8', '¥ 20', '¥ 90.5', '¥ 79', '¥ 68', '¥ 77.99', '¥ 76.5'];
            var con4 = ['豆捞坊100元代金券', '山林熟30元代金券', '争鲜美味菜品任选四样',
                '小辉哥100元代金券', '食之秘100元代金券', '七欣天88波格力蟹套餐', '望湘园100元代金券'
                , '蜀江烤鱼85抵100券'];
            $.each(img3, function (index, value) {
                var li4 = $("<li>").addClass('marbottom').appendTo('.shopinner');
                var a4 = $("<a>").appendTo(li4);
                $("<img>").attr('src', 'b/' + img4[index]).appendTo(a4);
                var div4 = $("<div>").appendTo(li4);
                $("<a>").text(con4[index]).appendTo(div4);
                $("<span>").text(money[index]).appendTo(div4);
            });


        }

    })


});


var LunarDate = {
    madd: new Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),
    HsString: '甲乙丙丁戊己庚辛壬癸',
    EbString: '子丑寅卯辰巳午未申酉戌亥',
    NumString: "一二三四五六七八九十",
    MonString: "正二三四五六七八九十冬腊",
    CalendarData: new Array(0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95),
    Year: null,
    Month: null,
    Day: null,
    TheDate: null,
    GetBit: function (m, n) {
        return (m >> n) & 1;
    },
    e2c: function () {
        this.TheDate = (arguments.length != 3) ? new Date() : new Date(arguments[0], arguments[1], arguments[2]);
        var total, m, n, k;
        var isEnd = false;
        var tmp = this.TheDate.getFullYear();
        total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + this.madd[this.TheDate.getMonth()] + this.TheDate.getDate() - 38;
        if (this.TheDate.getYear() % 4 == 0 && this.TheDate.getMonth() > 1) {
            total++;
        }
        for (m = 0; ; m++) {
            k = (this.CalendarData[m] < 0xfff) ? 11 : 12;
            for (n = k; n >= 0; n--) {
                if (total <= 29 + this.GetBit(this.CalendarData[m], n)) {
                    isEnd = true;
                    break;
                }
                total = total - 29 - this.GetBit(this.CalendarData[m], n);
            }
            if (isEnd)
                break;
        }
        this.Year = 1921 + m;
        this.Month = k - n + 1;
        this.Day = total;
        if (k == 12) {
            if (this.Month == Math.floor(this.CalendarData[m] / 0x10000) + 1) {
                this.Month = 1 - this.Month;
            }
            if (this.Month > Math.floor(this.CalendarData[m] / 0x10000) + 1) {
                this.Month--;
            }
        }
    },
    GetcDateString: function () {
        var tmp = "";
        //tmp += this.HsString.charAt((this.Year - 4) % 10);
        //tmp += this.EbString.charAt((this.Year - 4) % 12);
        //tmp += "年 ";
        if (this.Month < 1) {
            tmp += "(闰)";
            tmp += this.MonString.charAt(-this.Month - 1);
        } else {
            tmp += this.MonString.charAt(this.Month - 1);
        }
        tmp += "月";
        tmp += (this.Day < 11) ? "初" : ((this.Day < 20) ? "十" : ((this.Day < 30) ? "廿" : "三十"));
        if (this.Day % 10 != 0 || this.Day == 10) {
            tmp += this.NumString.charAt((this.Day - 1) % 10);
        }
        return tmp;
    },
    GetLunarDay: function (solarYear, solarMonth, solarDay) {
        if (solarYear < 1921 || solarYear > 2020) {
            return "";
        } else {
            solarMonth = (parseInt(solarMonth) > 0) ? (solarMonth - 1) : 11;
            this.e2c(solarYear, solarMonth, solarDay);
            return this.GetcDateString();
        }
    }
};
